package springdao.support;

import java.util.Properties;
import org.springframework.beans.factory.InitializingBean;
import springdao.DaoManager;
import springdao.DaoRepository;

/**
 * Equal to build a Properties bean like:</br>
 * 等同建立一個 Properties bean 如:<br/>
 * <div style="color:blue">
 * &lt;util:properties&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;save*&quot;&gt;PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;insert*&quot;&gt;PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;update*&quot;&gt;PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;bulk*&quot;&gt;PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;remove*&quot;&gt;PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;tx*&quot;&gt;PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;stx*&quot;&gt;PROPAGATION_SUPPORTS, ISOLATION_READ_COMMITTED, -Exception&lt;/prop&gt;<br/>
 * &nbsp;&nbsp;&lt;prop key=&quot;*&quot;&gt;PROPAGATION_NOT_SUPPORTED,ISOLATION_READ_COMMITTED,readOnly&lt;/prop&gt;<br/>
 * &lt;/util:properties&gt;
 * </div>
 * A DefaultTransactionAttributes bean will be given if {@link DaoManager @DaoManager}.{@link DaoManager#transactionAttributes() }
 * defined bean not found.<br/>
 * That's say: method name in {@link DaoRepository} start with save,insert,update,bulk,remove or tx will protected in transaction, 
 * and starts with stx will support transaction,and the others will not support transaction.
 * 如果找不到{@link DaoManager @DaoManager}.{@link DaoManager#transactionAttributes() }所定義的bean，
 * 則{@link DaoManager @DaoManager}會自動採用這個設定。<br/>
 * 也就是說{@link DaoRepository}以save、insert、update、bulk、remove或tx開頭的方法將被置入交易中，
 * 而以tx開頭的方法則僅支援交易，至於其它方法則不參與交易.
 * @author Kent Yeh
 */
public class DefaultTransactionAttributes extends Properties implements InitializingBean {

    private static final long serialVersionUID = 3852654438881074309L;

    public DefaultTransactionAttributes(Properties defaults) {
        super(defaults);
    }

    public DefaultTransactionAttributes() {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.put("persist", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("merge", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("save*", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("insert*", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("update*", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("bulk*", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("remove*", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("tx*", "PROPAGATION_REQUIRED, ISOLATION_READ_COMMITTED, -Exception");
        this.put("stx*", "PROPAGATION_SUPPORTS,ISOLATION_READ_COMMITTED, -Exception");
        this.put("*", "PROPAGATION_NOT_SUPPORTED,ISOLATION_READ_COMMITTED,readOnly");
    }
}
